<template>
  <div>
    <el-table :data="skuTableData" style="width: 100%" border>
      <el-table-column prop="id" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column prop="skuDefaultImg" label="默认图片" width="120">
        <template slot-scope="{row,$index}"> 
          <img :src="row.skuDefaultImg" alt="" style="width:120px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="100">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="100">
      </el-table-column>
      <el-table-column prop="prop" label="操作"> 
        <template slot-scope="{row}">
          <HintButton @click="onSale(row.id)" v-if="row.isSale==0" type="success" icon="el-icon-sort-down" size="mini" title="下架"></HintButton>
          <HintButton @click="CancleSale(row.id)" type="success" v-else icon="el-icon-sort-up" size="mini" title="上架"></HintButton>
          <HintButton type="primary" icon="el-icon-edit" size="mini" title="修改"></HintButton>
          <HintButton type="info" icon="el-icon-info" size="mini" title="详情"></HintButton>
          <HintButton @click="deleteSku(row.id)" type="danger" icon="el-icon-delete" size="mini" title="删除"></HintButton>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="text-align:center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total">
    </el-pagination>

  </div>
</template>

<script>
export default {
  name: "sku",
  data() {
    return {
      skuTableData: [],
      page: 1,
      limit: 3,
      total: 10,
    };
  },
  mounted(){
    this.getSkuList();
  },
  methods: {
    async getSkuList() {
      const { page, limit } = this;
      let result = await this.$API.sku.reqGetSkuList(page, limit);
      if (result.code == 200) {
        this.skuTableData = result.data.records;
        this.total=result.data.total;
      }
      // console.log(result);
    },
    handleSizeChange(val){
      this.limit=val;
      this.getSkuList();
    },
    handleCurrentChange(val){
      this.page=val;
      this.getSkuList();
    },
    // 上架
    async onSale(id){
      let result=await this.$API.sku.reqOnSale(id);
      if(result.code==200){
        this.$message({type:'success',message:'上架成功'});
        this.getSkuList();
      }
    },
     // 下架
    async CancleSale(id){
      let result=await this.$API.sku.reqCancelSale(id);
      if(result.code==200){
        this.$message({type:'success',message:'上架成功'});
        this.getSkuList();
      }
    },
    //删除sku数据
    async deleteSku(id){
      let result=await this.$API.sku.reqDeleteSku(id);
      if(result.code==200){
        this.$message({type:'success',message:'删除成功'});
        this.getSkuList(); 
      }
    }
  },
};
</script>

<style>
</style>